/*
 * BZPView.cpp
 *
 *  Created on: 24/05/2014
 *      Author: javier
 */

#include "BZPView.h"
#include "SDLUtils.h"
#include "BZPController.h"

BZPView::BZPView(SDLWindow* window, BZPModel* model) :
		menuView(this, window), scenarioView(this, model, window), waitingView(
				this, window), gameSelectView(this, window), decideGameStartView(
				this, window), infoView(this, window) {
	this->window = window;
	controller = NULL;
	state = MENU;
	score = "0";
}

void BZPView::render() {
	window->clear();
	if (state == MENU) {
		menuView.render();
	} else if (state == GAME) {
		scenarioView.render();
		SDL_Color color = { 255, 255, 255 };
		SDL_Texture *image = window->renderText(score.c_str(), color);
		window->renderTexture(image, 10, 10);
	} else if (state == GAME_SELECT || state == MATCH_SELECT) {
		gameSelectView.render();
	} else if (state == WAITING) {
		waitingView.render();
	} else if (state == DECIDE_GAME_START) {
		decideGameStartView.render();
	} else if (state == INFO) {
		infoView.render();
	}
	window->renderPresent();
}

void BZPView::update() {
	if (state == GAME) {
		scenarioView.update();
	}
}

void BZPView::setTarget(Positionable* target) {
	scenarioView.setTarget(target);
}

void BZPView::handleClick(int x, int y) {
	if (state == MENU) {
		menuView.handleClick(x, y);
	} else if (state == GAME_SELECT || state == MATCH_SELECT) {
		gameSelectView.handleClick(x, y);
	} else if (state == DECIDE_GAME_START) {
		decideGameStartView.handleClick(x, y);
	} else if (state == INFO) {
		infoView.handleClick(x, y);
	}
}

void BZPView::newGame() {
	controller->newGame();
}

void BZPView::joinGame() {
	controller->joinGame();
}

void BZPView::init(BZPController* controller) {
	this->controller = controller;
}

void BZPView::removeView(int id) {
	scenarioView.removeView(id);
}

void BZPView::clear() {
	scenarioView.clear();
}

void BZPView::changeToView(ViewStates state) {
	this->state = state;
}

void BZPView::exit() {
	controller->exit();
}

void BZPView::setScenarioDimensions(int width, int height) {
	scenarioView.setScenarioDimensions(width, height);
}

void BZPView::showCampaigns(std::string campaigns) {
	gameSelectView.loadCampaigns(campaigns);
	gameSelectView.showCampaigns();
	state = GAME_SELECT;
}

void BZPView::sendNewMatch(std::string id) {
	controller->sendSelectedMatch(id);
}

void BZPView::showInfo(InfoStates msj) {
	if (msj == END_GAME)
		infoView.setInfo(std::string("Fin de la partida"));
	else if (msj == NEXT_MATCH)
		infoView.setInfo(std::string("Comenzando la siguiente partida"));
	infoView.setState(msj);
	state = INFO;
}

void BZPView::notifyEndGame() {
	controller->sendResetGameMessage();
	controller->showMainMenu();
}

void BZPView::notifyNextMatch() {
	controller->loadNextScenario();
}

BZPView::~BZPView() {
}

void BZPView::add(Positionable* positionable) {
	scenarioView.add(positionable);
}

void BZPView::updateScore(std::string text) {
	score = text;
}

void BZPView::sendNewCampaign(std::string id, std::string name) {
	controller->sendSelectCampaign(id, name);
}

void BZPView::sendStartMatch(bool decide, std::string idPartida) {
	controller->sendStartMatch(decide, idPartida);
}

void BZPView::showPartidas(std::string partidas) {
	gameSelectView.loadMatchs(partidas);
	gameSelectView.showMatchs();
	state = MATCH_SELECT;
}

void BZPView::addExplosionView(Positionable* positionable) {
	scenarioView.addExplosion(positionable);
}

std::string BZPView::findAndRplace(std::string &s, std::string toReplace,
		std::string replaceWith) {
	return (s.replace(s.find(toReplace), toReplace.length(), replaceWith));
}
